#define _CRT_SECURE_NO_WARNINGS 1

#include "string.h"

namespace dr
{
	const size_t string::npos = -1;

	string& string::operator= (const char* s)
	{
		
		this->clear();
		*this += s;

		return *this;
	}

	//string& string::operator= (const string& str)
	//{
	//	if (_str == str._str)
	//	{
	//		return *this;
	//	}
	//	//size_t len = str._size;
	//	if (str._size > _capacity)
	//	{
	//		reserve(str._size > 2 * _capacity ? str._size : 2 * _capacity);
	//	}

	//	memcpy(_str, str._str, str._size + 1);

	//	_size = str._size;

	//	return *this;
	//}

	string& string::operator= (string str)
	{
		//if (_str != str._str)
		//if (this != &str)
		//{
		//	swap(str);
		//}
		swap(str);

		return *this;
	}

	void string::clear()
	{
		_size = 0;
		_str[0] = '\0';
	}

	const char* string::c_str() const
	{
		return _str;
	}

	size_t string::size() const
	{
		return _size;
	}

	size_t string::capacity() const
	{
		return _capacity;
	}

	string::iterator string::begin()
	{
		return _str;
	}
	string::const_iterator string::begin() const
	{
		return _str;
	}

	string::iterator string::end()
	{
		return _str + _size;
	}
	string::const_iterator string::end() const
	{
		return _str + _size;
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* str = new char[n + 1];
			_capacity = n;
			if (_str)
			{
				memcpy(str, _str, _size + 1);
				delete[] _str;
			}
			_str = str;
		}
	}

	char& string::operator[] (size_t pos)
	{
		return *(_str + pos);
	}

	const char& string::operator[] (size_t pos) const
	{
		return *(_str + pos);
	}

	void string::push_back(char c)
	{
		if (_size == _capacity)
		{
			reserve(2 * _capacity == 0 ? 4 : 2 * _capacity);
		}

		_str[_size] = c;
		_size++;
		_str[_size] = '\0';
	}

	string& string::operator+= (char c)
	{
		this->push_back(c);
		return *this;
	}

	string& string::operator+= (const char* s)
	{
		size_t len = strlen(s);
		if (_size + len > _capacity)
		{
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		}

		for (int i = 0; i <= len; i++)
		{
			_str[_size + i] = s[i];
		}
		_size += len;
		return *this;

	}

	string& string::append(const char* s)
	{
		//size_t len = strlen(s);
		//if (_size + len > _capacity)
		//{
		//	reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		//}

		//for (int i = 0; i <= len; i++)
		//{
		//	_str[_size + i] = s[i];
		//}
		//_size += len;
		//return *this;
		return *this += s;
	}

	void string::insert(size_t pos, const char* s)
	{
		size_t len = strlen(s);
		if (_size + len > _capacity)
		{
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		}

		//if (_size == _capacity)
		//{
		//	reserve(len > 2 * _capacity ? len : 2 * _capacity);
		//}

		size_t end = _size + len;

		//while (end >= pos)
		while (end > pos + len - 1)
		{
			_str[end] = _str[end - len];
			end--;
		}

		//memcpy(s, _str + pos, len);
		for(int i = 0; i < len; i++)
		{
			_str[pos + i] = *(s + i);
		}

		_size += len;
	}

	string& string::erase(size_t pos, size_t len)
	{
		if (_size - pos < len)
		{
			len = _size - pos;
		}

		for (size_t i = pos + len; i <= _size; i++)
		{
			_str[i - len] = _str[i];
		}

		return *this;
	}

	size_t string::find(char c, size_t pos)
	{
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i + pos] == c)
			{
				return i;
			}
		}

		return npos;
	}

	size_t string::find(const char* s, size_t pos) const
	{
		const char* ret = strstr(_str + pos, s);
		if (ret == nullptr)
		{
			return npos;
		}
		else
		{
			return ret - _str;
		}
	}

	string string::substr(size_t pos, size_t len) const
	{
		if (_size - pos < len)
		{
			len = _size - pos;
		}

		string ret;
		for (size_t i = 0; i < len; i++)
		{
			ret += _str[i + pos];
		}
		return ret;
	}

	void string::swap(string& s)
	{
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
		std::swap(_str, s._str);
	}

	void swap(string& x, string& y)
	{
		x.swap(y);
	}

	std::istream& operator>> (std::istream& is, string& str)
	{

		str.clear();
		char ch = is.get();
		
		while (ch != '\n' && ch != ' ')
		{
			str += ch;
			ch = is.get();
		}

		return is;
	}

	std::ostream& operator<< (std::ostream& os, const string& str)
	{
		for (size_t i = 0; i < str.size(); i++)
		{
			os << str[i];
		}

		return os;
	}

}